define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'union/guildhost/index',
                    table: 'guild_host_manage',
                },
                commonSearch: true,
                searchFormVisible: true,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: true,
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                columns: [
                    [
                        {field: 'guild_name', title: __('名称'), searchList:union_list, formatter: Table.api.formatter.normal},
                        {field: 'avatar', title: __('头像'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user_profile_id', title: __('用户ID')},
                        {field: 'nick_name', title: __('用户昵称')},
                        {field: 'online', title: __('在线状态'), searchList: {'0' : '离线','1' :'在线'}, formatter: Table.api.formatter.normal},
                        {field: 'user_status', title: __('用户状态'), searchList: {'0' : '正常','1' :'冻结','2':'注销'}, formatter: Table.api.formatter.normal},
                        {field: 'role', title: __('实名认证'), searchList: {'1' : '未认证','2' :'已认证'}, formatter: Table.api.formatter.normal},
                        {field: 'status', title: __('签约状态'), searchList: {'1' : '签约中','0' :'已解约'}, formatter: Table.api.formatter.normal},
                        {field: 'created_at', title: __('签约时间'), operate: false},
                        {field: 'unbind_at', title: __('解约时间'), operate: false},
                        {field: 'charm_value', title: __('魅力值'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons:[
                                {
                                    name: '移除',
                                    text: __('移除'),
                                    title: __('移除'),
                                    confirm: '确定移除用户吗？',
                                    classname: 'btn btn-xs btn-danger btn-magic btn-ajax',
                                    icon: 'fa fa-close',
                                    url: "union/guildhost/unbind",
                                    success: function (data, ret) {
                                        // console.log(ret, ret.message);
                                        //如果需要阻止成功提示，则必须使用return false;
                                        Layer.alert(ret.msg);
                                        $(".btn-refresh").trigger("click");

                                        return false;
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        $(".btn-refresh").trigger("click");
                                        return false;
                                    },

                                },

                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);

            $(".btn-share").click(function() {

                var html = $("#base_url").html();
                $("#hide").val(html); //这里可以获取动态数据赋值给$("#hide").val()
                $("#hide").select();
                try {
                    var state = document.execCommand("copy");
                } catch (err) {
                    var state = false;
                }
                if (state) {
                    layer.msg('复制成功',function (){
                        layer.open({
                            type: 1,
                            title: false,
                            area: ['500px', '80px'],
                            shadeClose: true,
                            content: "<p style='line-height: 70px'>"+html+"</p>"
                            ,yes: function(index){
                                layer.close(index);
                            }
                        });
                    })
                } else {
                    layer.msg('复制失败');
                }
            })



        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});